package com.simon.system.controller;

import java.util.List;

import com.simon.exception.OperationFailedException;
import com.simon.http.model.DoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simon.http.model.PResponse;
import com.simon.http.model.PageResponse;
import com.simon.system.model.Dept;
import com.simon.system.service.DeptService;
import com.simon.token.TokenValidate;
import com.simon.utils.SystemCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName DeptController
 * @Description 部门控制层
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/dept")
@Api(value = "dept", tags = {"部门接口"})
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "添加部门", notes = "根据Dept对象创建部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "dept", value = "部门对象", required = true, dataType = "Dept")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addDept(@RequestBody Dept dept) {
        Dept dDept = deptService.findByName(dept.getSimplename());
        if (dDept != null) {
            throw new OperationFailedException("该部门已存在");
        } else {
            //插入数据库
            int res = deptService.addDept(dept);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }
        }
    }

    @ApiOperation(value = "查询部门", notes = "根据id获取部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getDeptById(@RequestParam int id) {
        Dept dept = deptService.getDeptById(id);
        if (dept != null) {
            return new DoResponse<>(dept);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取部门列表", notes = "")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> findAllDept() {
        List<Dept> depts = deptService.findAllDepts();
        if (depts.size() > 0) {
            return new DoResponse<>(depts);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "查询部门列表", notes = "根据自定义条件分页查询部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "部门名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> queryDepts(@RequestParam String name, @RequestParam int pageNum, @RequestParam int pageSize) {
        List<Dept> depts = deptService.queryDepts(name, pageNum, pageSize);
        PResponse bPage = deptService.calPage(name, pageSize);
        if (depts != null) {
            return new PageResponse<>(depts,depts.size(),bPage.getTotalElements(),bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除部门", notes = "根据用户id删除部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteDept(@RequestParam int id) {
        int res = deptService.deleteDept(id);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新部门", notes = "更新部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "role", value = "部门对象", required = true, dataType = "Dept")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateDept(@RequestParam int id, @RequestBody Dept dept) {
        Dept dDept = deptService.getDeptById(id);
        if (dDept != null) {
            int res = deptService.updateDept(dDept, dept);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }
        } else {
            throw new OperationFailedException("不存在此数据");
        }
    }


    @ApiOperation(value = "查询子部门", notes = "根据pid获取子部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "上级部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/sublist", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> subDeptlist(@RequestParam int pid) {
        List<Dept> depts = deptService.sublist(pid);
        if (depts != null) {
            return new DoResponse<>(depts);
        } else {
            throw new OperationFailedException();
        }
    }
}
